export function formatDate(date, fmt) {
    if (!date) {
        return null
    }
    if (typeof (date) == "string") {
        return date;
    }
    if (typeof (date) == "number") {
        date = new Date(date);
    }
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
};

//获取两日期相差天数
export function dateDiff(sDate1, sDate2) {
    var date2 = typeof (sDate2) == 'string' ? new Date(Date.parse(sDate2.replace(/-/g, "/"))) : sDate2;
    var date1 = typeof (sDate1) == 'string' ? new Date(Date.parse(sDate1.replace(/-/g, "/"))) : sDate1;
    var iDays = parseInt(Math.abs(date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24) + 1;//如果起始日期和中止日期为同一天，相差天数为1天
    return iDays;
};

// 转换时间格式
export function fmtHour(timeStr) {
    if (typeof (timeStr) == 'string' && timeStr.length == 5) {
        return timeStr;
    }
    var timeData = new Date(timeStr)
    var hour = timeData.getHours().toString()
    if (hour.length == 1) {
        hour = '0' + hour
    }
    var min = timeData.getMinutes().toString()
    if (min.length == 1) {
        min = '0' + min
    }
    var newTimeStr = hour + ':' + min
    return newTimeStr
};


// 转换日期格式
export function fmtDate(timeStr) {
    var timeData = new Date(timeStr)
    var yearStr = timeData.getFullYear().toString()
    var monthStr = (timeData.getMonth() + 1).toString()
    if (monthStr.length == 1) {
        monthStr = '0' + monthStr
    }
    var dateStr = timeData.getDate().toString()
    if (dateStr.length == 1) {
        dateStr = '0' + dateStr
    }
    var newTimeStr = yearStr + '-' + monthStr + '-' + dateStr
    return newTimeStr
};


export function getKnowlageName(value) {
    if (value.count > 0) {
        var title, stuff
        title = value.title.substring(0, value.title.lastIndexOf('.'))
        stuff = value.title.substring(value.title.lastIndexOf('.'))
        return title + '(' + value.count + ')' + stuff;
    } else {
        return value.title;
    }
};


export function getTime(dataTime) {
    var date = new Date(dataTime);
    let day = date.getDate();
    let month = date.getMonth() + 1;
    let year = date.getFullYear();
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    let h = (date.getHours() <10?'0'+date.getHours():date.getHours())+ ':';
    let m = (date.getMinutes() <10?'0'+date.getMinutes():date.getMinutes())+ ':';
    let s = date.getSeconds() <10?'0'+date.getSeconds():date.getSeconds();
    let newDate = year + '-' + month + '-' + day + ' ' + h + m + s;
    return newDate;
}

export function getCurrentTime() {
    var date = new Date();
    let month = date.getMonth() + 1;
    let year = date.getFullYear();
    month = month < 10 ? '0' + month : month;

    let newDate = year + '-' + month
    return newDate;
}